import request from '@/utils/request';

export interface User {
  id?: number;
  username: string;
  password?: string;
  email?: string;
  phone?: string;
  avatar?: string;
  createTime?: string;
  updateTime?: string;
}

export interface LoginResponse {
  user: User;
  token: string;
}

// 用户登录
export async function login(username: string, password: string) {
  return request<LoginResponse>({
    url: '/user/login',
    method: 'POST',
    data: { username, password },
  });
}

// 用户注册
export async function register(data: Partial<User>) {
  return request<LoginResponse>({
    url: '/user/register',
    method: 'POST',
    data,
  });
}

// 获取当前用户信息
export async function getCurrentUser() {
  return request<User>({
    url: '/user/info',
    method: 'GET',
  });
}

// 发送验证码
export async function sendVerificationCode(phone: string) {
  return request<{ success: boolean }>({
    url: '/user/send-code',
    method: 'POST',
    params: { phone },
  });
}

// 验证验证码
export async function verifyCode(phone: string, code: string) {
  return request<{ success: boolean }>({
    url: '/user/verify-code',
    method: 'POST',
    params: { phone, code },
  });
}

// 更新用户信息
export async function updateUser(data: Partial<User>) {
  return request<User>({
    url: '/user/update',
    method: 'PUT',
    data,
  });
}

// 获取用户信息
export async function getUserById(id: number) {
  return request<User>({
    url: `/user/${id}`,
    method: 'GET',
  });
}